<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;

class CommentResource extends JsonResource
{
    protected $showSensitiveFields = false;

    public function toArray($request)
    {
        $user = auth('api')->user();
        if ($this->type == 1) {
            $article_content = $this->article->type == 1 ? Str::limit($this->article->content, 30) : $this->article->desc;
        } else {
            $article_content = Str::limit($this->topic->content, 30);
        }

        $data = [
//            'id' => $this->id,
            'avatar' => $user->avatar,
            'nickname' => $user->nickname,
            'created_at' => date('m月d日 H:i', strtotime($this->created_at)),
            'created_time' => strtotime($this->created_at),
            'article_id' => $this->type == 1 ? $this->article_id : $this->topic_id,
            'content' => $this->content,
            'type' => $this->type,
            'author_avatar' => $this->type == 1 ? $this->article->publisher->avatar : $this->topic->member->avatar,
            'article_content' => $article_content
        ];

        return $data;
    }
}
